from django.shortcuts import HttpResponse,redirect
from django.utils.deprecation import MiddlewareMixin


# class M1(MiddlewareMixin):
#     """中间件"""
#
#     def process_request(self, request):
#         print("M1，进来了")
#         return HttpResponse("无权访问")
#
#     def process_response(self, request, response):
#         print("M1,走了")
#         return response
#

class AuthMiddleware(MiddlewareMixin):
    """中间件"""

    def process_request(self, request):

        # 排除那些不需要登录就能访问的界面
        # request.path_info 获取当前用户请求的URL
        if request.path_info == '/login/':
            return
        # 1、读取当前用户的session信息
        info_dict = request.session.get("info")
        print(info_dict)
        if info_dict:
            return

        # 没有登录过,回到登录界面
        return redirect('/login/')

